package org.jeecg.modules.travel.controller;

import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.jeecg.modules.travel.dto.favorites.AppFavoritesPageDto;
import org.jeecg.modules.travel.dto.favorites.FavoritesAddDto;
import org.jeecg.modules.travel.dto.favorites.PcFavoritesPageDto;
import org.jeecg.modules.travel.entity.JhFavorites;
import org.jeecg.modules.travel.entity.JhFeedback;
import org.jeecg.modules.travel.service.IJhFavoritesService;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.travel.vo.favorites.AppFavoritesRecordVo;
import org.jeecg.modules.travel.vo.favorites.JhFavoritesVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

 /**
 * @Description: 用户收藏表
 * @Author: jeecg-boot
 * @Date:   2025-04-19
 * @Version: V1.0
 */
@Tag(name="用户收藏表")
@RestController
@RequestMapping("/travel/jhFavorites")
@Slf4j
public class JhFavoritesController extends JeecgController<JhFavorites, IJhFavoritesService> {
	@Autowired
	private IJhFavoritesService jhFavoritesService;
	@Autowired
	private ISysUserService sysUserService;
	/**
	 * 分页列表查询
	 *
	 * @param dto
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "用户收藏表-分页列表查询")
	@Operation(summary="用户收藏表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<JhFavoritesVo>> queryPageList(PcFavoritesPageDto dto, HttpServletRequest req) {
		JhFavorites favorites= new JhFavorites();
		BeanUtils.copyProperties(dto,favorites);
		QueryWrapper<JhFavorites> queryWrapper = QueryGenerator.initQueryWrapper(favorites, req.getParameterMap());
		Page<JhFavorites> page = new Page<>(dto.getPageNo(), dto.getPageSize());
		return Result.OK(jhFavoritesService.queryPageList(page, queryWrapper));
	}
	
	/**
	 *   添加
	 *
	 * @param jhFavorites
	 * @return
	 */
	@AutoLog(value = "用户收藏表-添加")
	@Operation(summary="用户收藏表-添加")
	@RequiresPermissions("org:jh_favorites:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody JhFavorites jhFavorites) {
		jhFavoritesService.save(jhFavorites);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param jhFavorites
	 * @return
	 */
	@AutoLog(value = "用户收藏表-编辑")
	@Operation(summary="用户收藏表-编辑")
	@RequiresPermissions("org:jh_favorites:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody JhFavorites jhFavorites) {
		jhFavoritesService.updateById(jhFavorites);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "用户收藏表-通过id删除")
	@Operation(summary="用户收藏表-通过id删除")
	@RequiresPermissions("org:jh_favorites:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		jhFavoritesService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "用户收藏表-批量删除")
	@Operation(summary="用户收藏表-批量删除")
	@RequiresPermissions("org:jh_favorites:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.jhFavoritesService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "用户收藏表-通过id查询")
	@Operation(summary="用户收藏表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<JhFavorites> queryById(@RequestParam(name="id",required=true) String id) {
		JhFavorites jhFavorites = jhFavoritesService.getById(id);
		if(jhFavorites==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(jhFavorites);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param jhFavorites
    */
    @RequiresPermissions("org:jh_favorites:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, JhFavorites jhFavorites) {
        return super.exportXls(request, jhFavorites, JhFavorites.class, "用户收藏表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequiresPermissions("org:jh_favorites:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, JhFavorites.class);
    }

	 /* =============================== 移动端服务 ============================================ */

	 @Operation(summary="用户收藏表-移动端分页列表查询")
	 @GetMapping(value = "/app/appFavoritesPageList")
	 public Result<IPage<AppFavoritesRecordVo>> appFavoritesPageList(AppFavoritesPageDto dto, HttpServletRequest req) {
		 JhFavorites favorites = new JhFavorites();
		 BeanUtils.copyProperties(dto,favorites);
		 String userId = getUserId();
		 QueryWrapper<JhFavorites> queryWrapper = QueryGenerator.initQueryWrapper(favorites, req.getParameterMap());
		 queryWrapper.lambda().eq(StringUtils.isNotBlank(userId), JhFavorites::getUserId,userId);
		 Page<JhFavorites> page = new Page<>(dto.getPageNo(), dto.getPageSize());
		 return Result.OK(jhFavoritesService.appFavoritesPageList(dto,page, queryWrapper));
	 }

	 @AutoLog(value = "用户收藏表-移动端收藏")
	 @Operation(summary="用户收藏表-移动端收藏")
	 //@RequiresRoles(value = {"app"})
	 //@RequiresPermissions("app:jh_favorites:storeUp")
	 @PostMapping(value = "/app/storeUp")
	 public Result<String> storeUp(@RequestBody @Valid FavoritesAddDto dto) {
		 dto.setUserId(getUserId());
		 String res = jhFavoritesService.storeUp(dto);
		 if (StringUtils.isNotBlank(res)) {
			 return Result.error(res);
		 }
		 return Result.OK("收藏成功!");
	 }

	 @AutoLog(value = "用户收藏表-移动端取消收藏")
	 @Operation(summary="用户收藏表-移动端取消收藏")
	 //@RequiresRoles(value = {"app"})
	 //@RequiresPermissions("app:jh_favorites:cancelStoreUp")
	 @GetMapping(value = "/app/cancelStoreUp")
	 public Result<String> cancelStoreUp(@RequestParam(name = "id",required = true) String id) {
		 String res = jhFavoritesService.cancelFavorites(id);
		 if (StringUtils.isNotBlank(res)) {
			 return Result.error(res);
		 }
		 return Result.OK("取消收藏成功!");
	 }

	 @AutoLog(value = "用户收藏表-移动端取消收藏")
	 @Operation(summary="用户收藏表-移动端取消收藏")
	 //@RequiresRoles(value = {"app"})
	 //@RequiresPermissions("app:jh_favorites:cancelStoreUp")
	 @GetMapping(value = "/app/cancelContent")
	 public Result<String> cancelContent(@RequestParam(name = "contentId",required = true) String contentId,
										 @RequestParam(name="type",required=true) String type) {
		 String res = jhFavoritesService.cancelContent(contentId,type,getUserId());
		 if (StringUtils.isNotBlank(res)) {
			 return Result.error(res);
		 }
		 return Result.OK("取消收藏成功!");
	 }

 }
